<!DOCTYPE html>
<html>
<!--
Copyright 2011 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
  <title>goog.ui.MenuItem Demo</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.array');
    goog.require('goog.debug.DivConsole');
    goog.require('goog.debug.LogManager');
    goog.require('goog.events');
    goog.require('goog.events.EventType');
    goog.require('goog.log');
    goog.require('goog.object');
    goog.require('goog.ui.CustomButton');
    goog.require('goog.ui.Menu');
    goog.require('goog.ui.MenuButton');
    goog.require('goog.ui.MenuItem');
    goog.require('goog.ui.Separator');
    goog.require('goog.ui.SubMenu');
    goog.require('goog.ui.decorate');
  </script>
  <link rel="stylesheet" href="css/demo.css">
  <link rel="stylesheet" href="../css/custombutton.css">
  <link rel="stylesheet" href="../css/menu.css">
  <link rel="stylesheet" href="../css/menuitem.css">
  <link rel="stylesheet" href="../css/menuseparator.css">
  <link rel="stylesheet" href="../css/menubutton.css">
  <style>
    /* Base class for all icon elements. */
    .icon {
      height: 16px;
      width: 16px;
      margin: 0 1px;
      background-image: url(../images/toolbar_icons.gif);
      background-repeat: no-repeat;
      vertical-align: middle;
    }

    /* "Format" icon. */
    .format-icon {
      background-position: -64px;
    }

    .positioning-frame {
      height: 250px;
      overflow: auto;
      width: 100%;
    }
  </style>
</head>
<body>
  <h1>goog.ui.MenuItem</h1>
  <table border="0" cellpadding="0" cellspacing="4" width="100%">
    <tbody>
      <tr valign="top">
        <td width="67%">
          <fieldset>
            <legend>
              Use the first letter of each menuitem to activate: &nbsp;
            </legend>
            <table border="0" cellpadding="0" cellspacing="4">
              <tbody>
                <tr valign="middle">
                  <td>
                    <div id="menuButtons"></div>
                  </td>
               </tr>
              </tbody>
            </table>
            <label>
            </label>
            <br>
          </fieldset>
       <td width="33%">
          <!-- Event log. -->
          <fieldset class="goog-debug-panel">
            <legend>Event Log</legend>
            <div id="log"></div>
          </fieldset>
        </td>
      </tr>
    </tbody>
  </table>
  <br>
  <div id="perf"></div>
  <script>
    var timer = goog.now();

    // Set up a logger.
    goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
    var logger = goog.log.getLogger('demo');
    var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
    logconsole.setCapturing(true);

    var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
    goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');

    function logEvent(e) {
      var component = e.target;
      var caption = (typeof component.getCaption == 'function') ?
          component.getCaption() : component.getId();
      goog.log.info(logger, '"' + caption + '" dispatched: ' + e.type);
    }

    // Create a sample menu.
    var m1 = new goog.ui.Menu();
    m1.setId('FileMenu');
    goog.array.forEach(['New', 'Open', 'Save', 'Save as', null, 'Change label'],
        function(label) {
          var item;
          if (label) {
            item = new goog.ui.MenuItem(label + '...');
            item.setId(label);
            item.setMnemonic(goog.events.KeyCodes[label.charAt(0)]);
            item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
            if (label == 'Save as') {
              item.setMnemonic(goog.events.KeyCodes.A);
              item.setEnabled(false);
            }
          } else {
            item = new goog.ui.MenuSeparator();
          }
          m1.addItem(item);
        });
    // Add a submenu.
    var s1 = new goog.ui.SubMenu('More');
    s1.setId('PrintSubmenu');
    s1.setMnemonic(goog.events.KeyCodes.M);
    goog.array.forEach(['Print', 'Send'], function(label) {
      var item = new goog.ui.MenuItem(label + '...');
      item.setId(label);
      item.setMnemonic(goog.events.KeyCodes[label.charAt(0)]);
      item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
      s1.addItem(item);
    });
    m1.addItem(s1);

    // Create the first button programmatically.
    var b1 = new goog.ui.MenuButton('File', m1);
    b1.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
    b1.setId('FileButton');
    b1.render(goog.dom.getElement('menuButtons'));
    b1.setTooltip('File menu demo');
    goog.events.listen(b1, EVENTS, logEvent);

    goog.events.listen(b1, goog.ui.Component.EventType.ACTION,
        function(e) {
          if (e.target && e.target.getCaption() == 'Change label...') {
            var label = window.prompt('Enter new menu label:', b1.getCaption());
            b1.setCaption(label || 'Empty');
          }
        });

    goog.dom.setTextContent(goog.dom.getElement('perf'),
        (goog.now() - timer) + 'ms');
  </script>
</body>
</html>
